package Q4;

import java.util.ArrayList;
import java.util.Scanner;

// 学生信息管理系统
public class StudentSystem {
    private ArrayList<Student> studentArr;

    public StudentSystem(ArrayList<Student> studentArr) {
        this.studentArr = studentArr;
    }

    public StudentSystem() {
    }


    private Student QueryID(int ID) {
        for (Student s1 : studentArr)
            if (s1.getID() == ID) {
                return s1;
            }
        return null;
    }

    public void addStudent(Student s) {
        if (QueryID(s.getID()) != null) {
            System.out.println("学号重复,添加失败！");
            return;
        }
        studentArr.add(s);
        System.out.println(s + "添加成功");
    }

    public void deleteStudent(Student s) {
        boolean flag = studentArr.remove(s);
        System.out.println(flag ? "删除成功" : "没有找到该学生");
    }

    //把src 学生 替换 dst 学生
    public void replaceStudent(Student src, Student dst) {
        if (QueryID(dst.getID()) != null) {
            studentArr.remove(dst);
            studentArr.add(src);
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败，没有这样的学生");
        }
    }

    //查找特定学生， 通过学号、姓名、年龄查找
    public ArrayList<Student> filterStudent(String by, String content) {
        ArrayList<Student> result = new ArrayList<Student>();
        for (Student s : studentArr) {
            switch (by) {
                case "ID": {
                    if (s.getID() == Integer.parseInt(content)) result.add(s);
                    break;
                }
                case "name": {
                    if (s.getName().equals(content)) result.add(s);
                    break;
                }
                case "age": {
                    if (s.getAge() == Integer.parseInt(content)) result.add(s);
                    break;
                }
                default: {
                    System.out.println("不支持的搜索类型");
                    return result;
                }
            }

        }
        return result;
    }

    //遍历所有元素
    public void show(ArrayList<Student> studentArr) {
        for (Student s : studentArr) {
            System.out.println("ID=" + s.getID() + ", name=" + s.getName() + ", age=" + s.getAge());
        }
    }

    private int getIntegerInput(Scanner sc, String field) {
        while (true) {
            System.out.println("请输入学生" + field);
            String input = sc.next();
            if (input.matches("[0-9]+")) {
                return Integer.parseInt(input);
            } else System.out.println("非法输入");
        }
    }

    private String getStringInput(Scanner sc, String field) {
        System.out.println("请输入学生" + field);
        return sc.next();
    }

    private void showMain() {
        System.out.println("------------欢迎使用学生信息管理系统--------------");
        System.out.println("                 主界面                         ");
        System.out.println("            （1）添加学生信息                    ");
        System.out.println("            （2）删除学生信息                    ");
        System.out.println("            （3）修改学生信息                    ");
        System.out.println("            （4）查找学生信息                    ");
        System.out.println("            （5）打印学生信息                    ");
        System.out.println("            （6）退出系统                    ");
        System.out.println("请输入数字进入相应的功能");
    }

    private void function1(Scanner sc) {
        int ID = getIntegerInput(sc, "ID");
        String name = getStringInput(sc, "姓名");
        int Age = getIntegerInput(sc, "年龄");
        addStudent(new Student(ID, name, Age));
    }

    private void function2(Scanner sc) {
        int ID = getIntegerInput(sc, "ID");
        for (Student s : studentArr) {
            if (ID == s.getID()) {
                deleteStudent(s);
                return;
            }
        }
        System.out.println("没有这样的学生");
    }

    private void function3(Scanner sc) {
        System.out.println("新学生：");
        int ID = getIntegerInput(sc, "ID");
        Student dst = QueryID(ID);
        if (dst != null) {
            String name = getStringInput(sc, "姓名");
            int Age = getIntegerInput(sc, "年龄");
            Student src = new Student(ID, name, Age);
            replaceStudent(src, dst);
        } else {
            System.out.println("没有找到到被修改的学生，取消操作");
        }

    }

    private void function4(Scanner sc) {
        System.out.println("(1)学号查找         （2）姓名查找         （3）年龄查找");
        switch (sc.nextInt()) {
            case 1: {
                System.out.println("请输入学号：");
                String content = sc.next();
                show(filterStudent("ID", content));
                break;
            }
            case 2: {
                System.out.println("请输入姓名：");
                String content = sc.next();
                show(filterStudent("name", content));
                break;
            }
            case 3: {
                System.out.println("请输入年龄：");
                String content = sc.next();
                show(filterStudent("age", content));
                break;
            }
        }
    }

    private void function5(Scanner sc) {
        show(studentArr);
    }

    private void waiting(Scanner sc) {
        System.out.println("输入任意字符继续。。。");
        sc.next();
    }
    public void run() {
        // 主界面
        Scanner sc = new Scanner(System.in);
        while (true) {
            showMain();
            switch (sc.nextInt()) {
                case 1: {
                    function1(sc);
                    break;
                }
                case 2: {
                    function2(sc);
                    break;
                }
                case 3: {
                    function3(sc);
                    break;
                }
                case 4: {
                    function4(sc);
                    break;
                }
                case 5: {
                    function5(sc);
                    break;
                }
                case 6:
                    {sc.close();return;}
            }
            waiting(sc);
        }
    }
}
